package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.Song;
import com.rjwh.dingdong.client.player.PlayState;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.widget.RoundImageView;

/**
 * @author Kay
 * @ClassName RadioAlbumProgramListAdapter
 * @Description
 * @date 2016-10-26
 * @history 2016-10-26 author: description:
 */

public class RadioAlbumProgramListAdapter extends AdapterBase<Song> {

    private Context mContext;
    private int selectIndex = -1;
    private PlayState playState = PlayState.READY;

    public RadioAlbumProgramListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.radio_album_program_list_item, null);
        }

        RoundImageView radioAlbumProgramListItemCoverImg = ViewHolder.get(convertView, R.id.radio_album_program_list_item_cover_img);
        final RoundImageView radioAlbumProgramListItemImgBtn = ViewHolder.get(convertView, R.id.radio_album_program_list_item_img_btn);
        TextView radioAlbumProgramListItemTitleTv = ViewHolder.get(convertView, R.id.radio_album_program_list_item_title_tv);
        TextView radioAlbumProgramListItemSubtitleTv = ViewHolder.get(convertView, R.id.radio_album_program_list_item_subtitle_tv);

        Song radioAlbumProgramData = getList().get(position);

        if (radioAlbumProgramData == null) {
            return convertView;
        }
        String programname = radioAlbumProgramData.getProgramname();
        String coverurl = radioAlbumProgramData.getCoverurl();
        String playcount = radioAlbumProgramData.getPlaycount();

        if (!AsStrUtils.isEmpty(programname)) {
            radioAlbumProgramListItemTitleTv.setText(programname);
        }
        if (!AsStrUtils.isEmpty(playcount)) {
            radioAlbumProgramListItemSubtitleTv.setText(String.format(mContext.getResources().getString(R.string.radio_album_play_times_format), playcount));
        }

        Glide.with(mContext).load(coverurl).dontAnimate().placeholder(R.drawable.icon_radio_program_cover_holder).error(R.drawable.icon_radio_program_cover_holder)
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(radioAlbumProgramListItemCoverImg);

        radioAlbumProgramListItemImgBtn.setImageResource(R.drawable.icon_radio_album_program_play);

        if (selectIndex == position) {
            switch (playState) {
                case READY:
//                    name_TV.setText("准备中 开始播放");
                    radioAlbumProgramListItemImgBtn.setImageResource(R.drawable.icon_radio_album_program_pause);
                    radioAlbumProgramListItemTitleTv.setTextColor(mContext.getResources().getColor(R.color.blue_button_nor));
                    break;
                case PLAY:
//                    name_TV.setText("播放中");
                    radioAlbumProgramListItemImgBtn.setImageResource(R.drawable.icon_radio_album_program_pause);
                    radioAlbumProgramListItemTitleTv.setTextColor(mContext.getResources().getColor(R.color.blue_button_nor));
                    break;
                case PAUSE:
//                    name_TV.setText("暂停");
                    radioAlbumProgramListItemImgBtn.setImageResource(R.drawable.icon_radio_album_program_play);
                    radioAlbumProgramListItemTitleTv.setTextColor(mContext.getResources().getColor(R.color.blue_button_nor));
                    break;
                case STOP:
//                    name_TV.setText("停止");
                    radioAlbumProgramListItemImgBtn.setImageResource(R.drawable.icon_radio_album_program_play);
                    radioAlbumProgramListItemTitleTv.setTextColor(mContext.getResources().getColor(R.color.content_text_color));
                    break;
            }
        }else {
            radioAlbumProgramListItemTitleTv.setTextColor(mContext.getResources().getColor(R.color.content_text_color));
        }

        return convertView;
    }
    public void setSelectIndex(int i,PlayState playState) {
        selectIndex = i;
        this.playState = playState;
        Log.i("HTML", "selectIndex:" + selectIndex+" playState:"+playState);
        notifyDataSetChanged();
    }

    public void setSelectIndex(PlayState playState) {
        this.playState = playState;
        notifyDataSetChanged();
    }

}
